package com.mmy.service.car.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mmy.controller.Common;
import com.mmy.dao.car.TCarOrderRepository;
import com.mmy.dao.car.VCarOrderRepository;
import com.mmy.domain.car.TCarOrder;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.car.CarOrderService;

@Service
public class CarOrderServiceImpl implements CarOrderService {

	@Autowired
	TCarOrderRepository tCarOrderRepository;
	@Autowired
	VCarOrderRepository vCarOrderRepository;

	/**
	 * 个人中心通过登录人获取车辆预留 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapCarReserve(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tCarOrderRepository.countByUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取车辆退单 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapCarRefund(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tCarOrderRepository.countByRefundUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取车辆订单 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapCarOrder(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, tCarOrderRepository.countByOrderUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取车辆预留
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<TCarOrder> findByUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<TCarOrder> lstCarOrder = new ArrayList<TCarOrder>();
		lstCarOrder = tCarOrderRepository.findByUserID(iUserID, iTempPageNo, iPageSize);
		return lstCarOrder;
	}

	/**
	 * 个人中心通过登录人获取车辆订单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<TCarOrder> findByOrderUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<TCarOrder> lstCarOrder = new ArrayList<TCarOrder>();
		lstCarOrder = tCarOrderRepository.findByOrderUserID(iUserID, iTempPageNo, iPageSize);
		return lstCarOrder;
	}

	/**
	 * 个人中心通过登录人获取车辆退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<TCarOrder> findByChargeBackUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<TCarOrder> lstCarOrder = new ArrayList<TCarOrder>();
		lstCarOrder = tCarOrderRepository.findByChargeBackUserID(iUserID, iTempPageNo, iPageSize);
		return lstCarOrder;
	}

	/**
	 * 个人中心通过登录人取消车辆预留
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TCarOrder dbCarOrder = tCarOrderRepository.findByID(iID);
		dbCarOrder.setOrderState((byte) 3);
		dbCarOrder.setModifyDate(new Date());
		dbCarOrder.setReviser(sessionSysUser.getUserName());
		tCarOrderRepository.save(dbCarOrder);
	}

	/**
	 * 个人中心通过登录人申请车辆退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateRefundState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TCarOrder dbCarOrder = tCarOrderRepository.findByID(iID);
		dbCarOrder.setOrderState((byte) 4);
		dbCarOrder.setRefundProcess((byte) 1);
		dbCarOrder.setModifyDate(new Date());
		dbCarOrder.setReviser(sessionSysUser.getUserName());
		tCarOrderRepository.save(dbCarOrder);
	}

	/**
	 * 个人中心通过登录人点击撤销退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateCancelState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TCarOrder dbCarOrder = tCarOrderRepository.findByID(iID);
		dbCarOrder.setOrderState((byte) 2);
		dbCarOrder.setRefundProcess((byte) 0);
		dbCarOrder.setModifyDate(new Date());
		dbCarOrder.setReviser(sessionSysUser.getUserName());
		tCarOrderRepository.save(dbCarOrder);
	}

	/**
	 * 通过订单ID查询
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public TCarOrder findByID(int iID) {
		// TODO Auto-generated method stub
		return tCarOrderRepository.findByID(iID);
	}
}
